package com.niit.xlztest.controller;

import com.niit.xlztest.bean.Book;
import com.niit.xlztest.bean.Comm;
import com.niit.xlztest.repository.CommRepository;
import com.niit.xlztest.service.CommService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Controller
public class CommentsController {
    @Autowired
    private CommService commService;

    private CommRepository commRepository;

//    @RequestMapping("/getComments")
//    @ResponseBody
//    public List<Comm> getComments(){
//        List<Comm> comms = commService.findAllComms();
//        return comms;
//    }
//    @PostMapping("/addComment")
//    @ResponseBody
//    public Comm addComment(@RequestBody Comm comm) {
//        Comm newComm = new Comm();
//        newComm.setComm(comm.getComm());
//        commRepository.save(newComm);
//        return newComm;
//    }
//    @GetMapping("/goToComm")
//    public String goToComm() {
//        return "Test_Comm_WebSocket";
//    }
    @GetMapping("/goToComm")
    public String goToComm(Model model) {
        return "Test_Comm_Ajax";
    }
    @RequestMapping(value = "/getComments", method = RequestMethod.GET)
    @ResponseBody
    public List<Comm> getComments(Model model) {
        List<Comm> comments = commService.findAllComms();
        return comments;
    }

    @RequestMapping(value = "/addComment", method = RequestMethod.POST)
    @ResponseBody
    public Comm addComment(@RequestParam("content") String content) {
//        Comm comment = new Comm();
//        comment.setComm(content);
//        commService.addComm(comment);
        Comm comment = new Comm();
        // 将前端对象中的属性复制到新对象中
        BeanUtils.copyProperties(comment, comment);
        // 手动设置其他属性
        comment.setUpdate_date(LocalDateTime.now());
        comment.setCreated_date(LocalDateTime.now());
        comment.setVersion(1);
        comment.setUser_id("003");
        comment.setComm_like("2");
        comment.setComm(content);
        commService.addComm(comment);
        return comment;
    }
}
